home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / unix / getenv.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  3KB  |  120 lines

  1.  
  2. /*
  3.  *  GETENV.C
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/libraries.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16.  
  17. #if INCLUDE_VERSION >= 36
  18. #include <dos/dostags.h>
  19. #include <clib/dos_protos.h>
  20. #endif
  21.  
  22. typedef struct Library Library;
  23.  
  24. typedef struct Sym {
  25.     struct Sym *sm_Next;
  26.     char       *sm_VarName;
  27.     char       *sm_Data;
  28.     long    sm_DataLen;
  29. } Sym;
  30.  
  31. Sym *_Env_SymBase = NULL;
  32.  
  33. extern Library *SysBase;
  34.  
  35. char *
  36. getenv(varName)
  37. const char *varName;
  38. {
  39.     Sym *sym;
  40.  
  41.     for (sym = _Env_SymBase; sym; sym = sym->sm_Next) {
  42.     if (stricmp(varName, sym->sm_VarName) == 0)
  43.         break;
  44.     }
  45.     if (sym == NULL) {
  46.     if (sym = malloc(sizeof(Sym) + strlen(varName) + 1)) {
  47.         sym->sm_DataLen = -1;
  48.         sym->sm_VarName = (char *)(sym + 1);
  49.         strcpy(sym->sm_VarName, varName);
  50.  
  51. #if INCLUDE_VERSION >= 36
  52.         if (SysBase->lib_Version >= 37) {
  53.         char buf[2];
  54.         long len;
  55.  
  56.         if (GetVar(varName, buf, sizeof(buf) - 1, 0) >= 0) {
  57.             len = IoErr();
  58.             if (sym->sm_Data = malloc(len + 1)) {
  59.             if ((len = GetVar(varName, sym->sm_Data, len + 1, 0)) >= 0) {
  60.                 sym->sm_DataLen = len;
  61.             } else {
  62.                 free(sym->sm_Data);
  63.             }
  64.             }
  65.         }
  66.         } else
  67. #endif
  68.         {
  69.         char *ptr = malloc(strlen(varName) + 8);
  70.         long fh;
  71.  
  72.                 if (ptr)
  73.                 {
  74.                     strcpy(ptr, "ENV:");
  75.                     strcat(ptr, varName);
  76.                     if (fh = Open(ptr, 1005)) {
  77.                         long len;
  78.  
  79.                         free(ptr);
  80.                         Seek(fh, 0L, 1);
  81.                         if ((len = Seek(fh, 0L, 0)) >= 0) {
  82.                             Seek(fh, 0L, -1);
  83.                             if( (sym->sm_Data = malloc(len + 1)) != NULL)
  84.                             {
  85.                                 if ((len = Read(fh, sym->sm_Data, len)) >= 0) {
  86.                                     sym->sm_DataLen = len;
  87.                                 } else {
  88.                                     free(sym->sm_Data);
  89.                                 }
  90.                             }
  91.                         }
  92.                         Close(fh);
  93.                     } else {
  94.                         free(ptr);
  95.                     }
  96.                 }
  97.         }
  98.         {
  99.         long len;
  100.  
  101.         if ((len = sym->sm_DataLen) >= 0) {
  102.             sym->sm_Next = _Env_SymBase;
  103.             _Env_SymBase = sym;
  104.             if (len && sym->sm_Data[len-1] == '\n')
  105.             --len;
  106.             sym->sm_Data[len] = 0;
  107.         } else {
  108.             free(sym);
  109.             sym = NULL;
  110.         }
  111.         }
  112.     }
  113.     }
  114.     if (sym)
  115.     return(sym->sm_Data);
  116.     return(NULL);
  117. }
  118.  
  119.  
  120.